package com.wsh.controller.generator;

import com.wsh.entity.generator.GenBaseConfEntity;
import com.wsh.entity.generator.GenFileConfEntity;
import com.wsh.generator.GenBaseConfService;
import com.wsh.generator.GenConfService;
import com.wsh.generator.GenFileConfService;
import com.wsh.generator.GenerCodeService;
import com.wsh.utils.JsonResult;
import com.wsh.vo.generator.TablesConfVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据表api接口
 */
@RestController
@RequestMapping("/gengerCode")
public class GenerCodeController {

    @Autowired
    private GenerCodeService generCodeService;

    @Autowired
    private GenConfService genConfService;

    @Autowired
    private GenBaseConfService genBaseConfService;

    @Autowired
    private GenFileConfService genFileConfService;

    @GetMapping("/list")
    public Object list(@RequestParam Map<String, Object> params) {
        return JsonResult.success(generCodeService.list(params));
    }

    @GetMapping("/getColumn")
    public Object getColumn(@RequestParam String tableName) {
        Map<String, Object> maps = new HashMap<>();
        Object confList = genConfService.getConfList(tableName);
        GenBaseConfEntity baseConf = genBaseConfService.getById(tableName);
        List<GenFileConfEntity> fileConf = genFileConfService.getByTableName(tableName);
        maps.put("confList", confList);
        maps.put("baseConf", baseConf);
        maps.put("fileConf", fileConf);
        return JsonResult.success(maps);
    }

    @GetMapping("/asynTable")
    public Object asyncTable(@RequestParam String tableName) {
        return JsonResult.success(generCodeService.queryColumns(tableName));
    }

    @PostMapping("/configTable")
    public Object configTable(@RequestBody TablesConfVo tablesConfVo) {
//        System.out.println(tablesConfVo);
        genConfService.saveConf(tablesConfVo);
        return JsonResult.success(true);
    }

    @GetMapping("/genCode")
    public Object genCode(@RequestParam String tableName) {
        try {
            return JsonResult.success(genConfService.genCode(tableName));
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.fail(e.getMessage());
        }
    }

}
